{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Reorg Layer Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 4, 1)\n",
      "(1, 1, 2, 4)\n"
     ]
    }
   ],
   "source": [
    "batch_size, height, width, channels = 1, 2, 4, 1\n",
    "stride = 2\n",
    "\n",
    "_height, _width, _channel = height // stride, width // stride, channels * stride * stride\n",
    "\n",
    "input = np.array([[[[0], [2], [1], [3]], [[4], [6], [5], [7]]]])\n",
    "\n",
    "net = input\n",
    "net = tf.reshape(net, [batch_size, _height, stride, _width, stride, channels])\n",
    "net = tf.transpose(net, [0, 1, 3, 2, 4, 5])  # batch_size, _height, _width, stride, stride, channels\n",
    "net = tf.reshape(net, [batch_size, _height, _width, stride * stride * channels], name='reorg')\n",
    "\n",
    "output = net.eval(session=tf.Session())\n",
    "\n",
    "print(input.shape)\n",
    "print(output.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[0],\n",
       "         [2],\n",
       "         [1],\n",
       "         [3]],\n",
       "\n",
       "        [[4],\n",
       "         [6],\n",
       "         [5],\n",
       "         [7]]]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[0, 2, 4, 6],\n",
       "         [1, 3, 5, 7]]]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert input[0,0,0,0] == output[0,0,0,0]\n",
    "assert input[0,0,1,0] == output[0,0,0,1]\n",
    "assert input[0,0,2,0] == output[0,0,1,0]\n",
    "assert input[0,0,3,0] == output[0,0,1,1]\n",
    "assert input[0,1,0,0] == output[0,0,0,2]\n",
    "assert input[0,1,1,0] == output[0,0,0,3]\n",
    "assert input[0,1,2,0] == output[0,0,1,2]\n",
    "assert input[0,1,3,0] == output[0,0,1,3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Read darknet .weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "weight_array = np.fromfile('/data/darknet/yolov2.weights', dtype='f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50983565,)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weight_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
